//
//  KTableData.h
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "NSObject+KTableModel.h"
#import "KCellModelConfig.h"
#import "KSectionModelConfig.h"
#import <UIKit/UIKit.h>

#define KTableDataSelPrefix @"K_"

@protocol KTableDataCellProtocol <NSObject>

@optional
+ (void)registerToTable:(UITableView *)table;
+ (void)registerToTable:(UITableView *)table delegate:(id)delegate;
- (void)willDisplayCell;
- (void)didEndDisplayCell;

@end

@protocol KTableConfigure <NSObject>

/**
 *  注册TableViewCell相关设置
 */

- (void)registerCellList:(NSArray<Class> *)cellList delegate:(id)delegate;

//默认的reuseIdentifier class类名
- (void)registerCellClass:(Class)aClass config:(KCellModelConfig *)config;
- (void)registerSectionHeaderClass:(Class)aClass config:(KSectionModelConfig *)config;
- (void)registerSectionFooterClass:(Class)aClass config:(KSectionModelConfig *)config;

//指定reuseIdentifier
- (void)registerClass:(Class)aClass forCellReuseIdentifier:(NSString *)identifier config:(KCellModelConfig *)config;
- (void)registerClass:(Class)aClass forSectionHeaderViewReuseIdentifier:(NSString *)identifier config:(KSectionModelConfig *)config;
- (void)registerClass:(Class)aClass forSectionFooterViewReuseIdentifier:(NSString *)identifier config:(KSectionModelConfig *)config;

/**
 *  是否检查编辑状态，默认是NO
 */
@property (nonatomic) BOOL enableEdit;

//是否开启
@property (nonatomic) BOOL enableIndexTitles;

//自动设置cell的selectionStyle为UITableViewCellSelectionStyleNone
@property (nonatomic) BOOL noCellSelectionStyle;

@end

@protocol KTableDataSource <NSObject>

- (void)setModelList:(NSArray *)modelList forSection:(NSInteger)section;
- (void)setSectionModel:(id)model forSection:(NSInteger)section;
- (NSArray *)filterRepeatObjects:(NSArray *)objects section:(NSInteger)section;
- (NSMutableArray *)dataArrayOfSection:(NSInteger)section;
- (id)modelAtIndexPath:(NSIndexPath *)indexPath;
- (id)sectionModelForSection:(NSInteger)section;
- (NSInteger)sectionForSectionModel:(id)sectionModel;
- (NSIndexPath *)replaceModel:(id)oldModel withModel:(id)newModel section:(NSInteger)section;
- (NSArray<NSIndexPath *> *)addObjects:(NSArray *)objects afterSection:(NSInteger)section checkRepeat:(BOOL)checkRepeat;
- (NSArray<NSIndexPath *> *)addObjects:(NSArray *)objects beforeSection:(NSInteger)section checkRepeat:(BOOL)checkRepeat;
- (void)cleanData;
- (void)cleanSectionData;

- (NSIndexPath *)indexPathForModel:(id)model;

@end

@protocol KTableOperation <NSObject>

@property (nonatomic) UITableViewRowAnimation rowAnimation;

- (void)beginUpdates;
- (void)endUpdates;
- (void)reloadData;

- (void)reloadWithDataArrayList:(NSArray<NSArray *> *)dataArrayList;

- (void)insertObjects:(NSArray *)objects inRange:(NSRange)range section:(NSInteger)section;
- (void)insertObject:(id)object atIndex:(NSInteger)index section:(NSInteger)section;
- (void)insertObjects:(NSArray *)objects beforeSection:(NSInteger)section checkRepeat:(BOOL)checkRepeat;
- (void)insertObjects:(NSArray *)objects afterSection:(NSInteger)section checkRepeat:(BOOL)checkRepeat;
- (void)insertObjects:(NSArray *)objects afterObject:(id)object section:(NSInteger)section checkRepeat:(BOOL)checkRepeat;
- (void)insertObjects:(NSArray *)objects beforeObject:(id)object section:(NSInteger)section checkRepeat:(BOOL)checkRepeat;
- (void)removeObjectsInRange:(NSRange)range section:(NSInteger)section;
- (void)removeObjects:(NSArray *)objects inSection:(NSInteger)section;
- (void)removeObject:(id)object inSection:(NSInteger)section;
- (void)removeObjectsWithIndexSet:(NSIndexSet *)indexSet inSection:(NSInteger)section;
- (void)reloadModel:(id)model section:(NSInteger)section;
- (void)reloadModel:(id)model atIndexPath:(NSIndexPath *)indexPath;
//- (void)reloadModelList:(NSArray *)modelList section:(NSInteger)section;
- (void)reloadModel:(id)oldModel replace:(id)newModel section:(NSInteger)section;
- (void)reloadModelList:(NSArray *)modelList replace:(NSArray *)newModelList section:(NSInteger)section;
- (void)reloadSection:(NSInteger)section withModelList:(NSArray *)modelList;
- (void)reloadSectionModel:(id)sectionModel section:(NSInteger)section;

- (NSIndexPath *)refreshModel:(id)model section:(NSInteger)section;
- (NSIndexPath *)refreshModel:(id)model;
- (void)refreshSectionModelAtSection:(NSInteger)section;
- (void)refreshSectionModel:(id)sectionModel;

- (void)reloadVisibleRows;

- (void)selectObject:(id)object animated:(BOOL)animated position:(UITableViewScrollPosition)position;
- (void)deSelectObject:(id)object animated:(BOOL)animated;

@end

@interface KTableData : NSObject <UITableViewDelegate,UITableViewDataSource>

//@property (strong,nonatomic) NSObject * extendDelegate;

///如果不需要自己实现额外功能，delegate和datasource传nil
- (instancetype)init;
- (instancetype)initWithDelegate:(id)delegate dataSource:(id)dataSource;
- (void)setDelegate:(id)delegate;
- (id<KTableConfigure>)configure;
- (id<KTableDataSource>)dataConfig;
- (id<KTableOperation>)operation;

- (void)applyToTable:(UITableView *)tableView;

@end

@interface UITableView (KTableData)

- (KTableData *)kTableData;
- (id<KTableConfigure>)configure;
- (id<KTableDataSource>)dataConfig;
- (id<KTableOperation>)operation;

@end
